package com.ihrm.system.controller;

import com.ihrm.common.entity.Result;
import com.ihrm.common.entity.ResultCode;
import com.ihrm.common.exception.CommonException;
import com.ihrm.domain.system.Permission;
import com.ihrm.system.service.PermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @auther chongxiongwei
 * @create 2020-04-20-11:17
 */
@Api(tags = {"权限控制"})
@CrossOrigin
@RestController
@RequestMapping("/sys")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;


    /**
     * 保存权限
     */
    @PostMapping("/permission")
    public Result save(@RequestBody Map<String,Object> map) throws Exception {

        permissionService.save(map);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 修改权限
     */
    @PutMapping("/permission/{id}")
    public Result update(@PathVariable("id")String id,@RequestBody Map<String,Object> map) throws Exception {
        map.put("id",id);
        permissionService.update(map);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 根据权限ID删除权限
     */
    @DeleteMapping("/permission/{id}")
    public Result delete(@PathVariable("id")String id) throws CommonException {
        permissionService.delete(id);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 根据ID查看权限
     */
    @GetMapping("/permission/{id}")
    public Result findById(@PathVariable("id")String id) throws CommonException {
        Map map  = permissionService.findById(id);
        return new Result(ResultCode.SUCCESS,map);
    }

    /**
     * 查看所有权限
     */
    @GetMapping("/permission")
    public Result findByPage(@RequestParam Map<String,Object> map){
        List<Permission> permissionList = permissionService.findAll(map);
        return new Result(ResultCode.SUCCESS,permissionList);
    }

}
